<?php
General::getTpl()->assign('extraTitle',"Seitenbox-Inhalt");

General::getTpl()->assign('showExtraBBCode',true);

if(!isset($_GET['boxId']) || !is_numeric($_GET['boxId'])) exit;
General::getTpl()->assign('boxId',$_GET['boxId']);

if(!Admin::checkPermission('canManagePages')) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{
	/**
	 * NEW Content
	 */
	if(isset($_GET['do']) && $_GET['do'] == "new") {
		// Form-Data
		$formField = array();
		$i = 0;
		$formField[$i]['fieldName'] = "Titel";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = false;
		$i++;
	
		$formField[$i]['fieldName'] = "Text";
		$formField[$i]['formName'] = "text";
		$formField[$i]['fieldDisplayBBCodeSmilies'] = true;
		$formField[$i]['fieldSelectOptions'] = "55_20";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXTAREA;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = false;
		$i++;
		
		
		$predefLinkText = "Keine Datei::--::";
		$predefLinks = Pages::getPageLinks();
		foreach($predefLinks as $preLink) {
			$predefLinkText .= $preLink['linkName']."::--::";
		}
		// Delete last comma
		$predefLinkText = substr($predefLinkText,0,strlen($predefLinkText)-6);
		
		$formField[$i]['fieldName'] = "Datei";
		$formField[$i]['formName'] = "file";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = "";
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = $predefLinkText;
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
		
		
		$form = new Form();
		$form->initByData("Inhalt erstellen",true,"Speichern!",$formField,false,true,false);
		
		// Done?
		$check = $form->completeCheck();
		if($check === "preview") {
			$parsedText = nl2br(htmlspecialchars($_POST['text']));
			$parsedText = BBCode::getInstance()->parse(nl2br($parsedText),true);
			$parsedText = Smilies::getInstance()->parse($parsedText);
			
			General::getTpl()->assign('title',$_POST['Titel']);
			General::getTpl()->assign('text',$parsedText);
			
			General::getTpl()->assign('preview',
				General::getTpl()->fetch('../admin/pagePreview.html'));
			
			
			$form->outputFormular('form','formular.html');
		}elseif($check) {
			
			
			$file = "";
			$get = "";
			// PredefinedLink selected
			foreach($predefLinks as $preL) {
				if($preL['linkName'] == $_POST['file'][0]) {
					$file = $preL['linkFile'];
					$get = $preL['linkGetParameters'];
				}
			}
			
			// Create Box
			Pages::createBoxContent($_GET['boxId'],$_POST['Titel'],$_POST['text'],$file,$get);
			
			General::ouputMessage('Erfolgreich','Der Inhalt wurde erfolgreich gespeichert.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	/**
	 * Edit Content
	 */
	if(isset($_GET['do']) && $_GET['do'] == "edit" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		// Get data
		$query = DB::getInstance()->query("
			SELECT
				boxId,
				conTitle,
				conText,
				conFile,
				conFileGetvars
			FROM
				".Tables::$pageBoxContentTbl."
			WHERE
				id = '".$_GET['id']."'");
		$data = DB::getInstance()->fetchAssoc($query);
		
		// Form-Data
		$formField = array();
		$i = 0;
		$formField[$i]['fieldName'] = "Titel";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = $data['conTitle'];
		$formField[$i]['fieldCheck'] = false;
		$i++;
	
		$formField[$i]['fieldName'] = "Text";
		$formField[$i]['formName'] = "text";
		$formField[$i]['fieldDisplayBBCodeSmilies'] = true;
		$formField[$i]['fieldSelectOptions'] = "55_20";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXTAREA;
		$formField[$i]['fieldDefaultValue'] = htmlspecialchars_decode($data['conText']);
		$formField[$i]['fieldCheck'] = false;
		$i++;
		
		
		$selected = "";
		$predefLinkText = "Keine Datei::--::";
		$predefLinks = Pages::getPageLinks();
		foreach($predefLinks as $preLink) {
			if($data['conFile'] == $preLink['linkFile'] && $data['conFileGetvars'] == $preLink['linkGetParameters']) $selected = $preLink['linkName'];
			$predefLinkText .= $preLink['linkName']."::--::";
		}
		// Delete last comma
		$predefLinkText = substr($predefLinkText,0,strlen($predefLinkText)-6);
		
		$formField[$i]['fieldName'] = "Datei";
		$formField[$i]['formName'] = "file";
		$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
		$formField[$i]['fieldDefaultValue'] = $selected;
		$formField[$i]['fieldCheck'] = false;
		$formField[$i]['fieldSelectOptions'] = $predefLinkText;
		$formField[$i]['fieldSelectMultiple'] = false;
		$i++;
		
		
		$form = new Form();
		$form->initByData("Inhalt bearbeiten",true,"Speichern!",$formField,false,true,false);
		
		// Done?
		$check = $form->completeCheck();
		if($check === "preview") {
			$parsedText = nl2br(htmlspecialchars($_POST['text']));
			$parsedText = BBCode::getInstance()->parse(nl2br($parsedText),true);
			$parsedText = Smilies::getInstance()->parse($parsedText);
			
			General::getTpl()->assign('title',$_POST['Titel']);
			General::getTpl()->assign('text',$parsedText);
			
			General::getTpl()->assign('preview',
				General::getTpl()->fetch('../admin/pagePreview.html'));
			
			
			$form->outputFormular('form','formular.html');
		}elseif($check) {
			
			
			$file = "";
			$get = "";
			// PredefinedLink selected
			foreach($predefLinks as $preL) {
				if($preL['linkName'] == $_POST['file'][0]) {
					$file = $preL['linkFile'];
					$get = $preL['linkGetParameters'];
				}
			}
			
			// Create Box
			Pages::editBoxContent($_GET['id'],$_GET['boxId'],$_POST['Titel'],$_POST['text'],$file,$get);
			
			General::ouputMessage('Erfolgreich','Der Inhalt wurde erfolgreich gespeichert.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	

	
	if(isset($_GET['do']) && $_GET['do'] == "del" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete navigation
				Pages::deleteBoxContent($_GET['id']);
			}
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	
	
	
if(isset($_GET['do']) && $_GET['do'] == "up" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		$unsortedBoxes = Pages::getBoxContent($_GET['boxId']);
		// Create Array
		$unsortedArray = array();
		foreach($unsortedBoxes as $data) {
			$unsortedArray[] = $data['id'];
		}
		
		$sortedArray = General::orderArray($unsortedArray,"up",$_GET['id']);
		if($sortedArray == false) {
			General::ouputMessage('Fehler','Die Box ist schon ganz links.','form');
		}else{
			// Save
			foreach($sortedArray as $orderId=>$id) {
				DB::getInstance()->query("
					UPDATE
						".Tables::$pageBoxContentTbl."
					SET
						orderId = '".$orderId."'
					WHERE
						id = '".$id."'");
			}
			General::ouputMessage('Erfolgreich','Der Content wurde erfolgreich verschoben.','form');
		}
	}
	
	if(isset($_GET['do']) && $_GET['do'] == "down" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		$unsortedBoxes = Pages::getBoxContent($_GET['boxId']);
		// Create Array
		$unsortedArray = array();
		foreach($unsortedBoxes as $data) {
			$unsortedArray[] = $data['id'];
		}
		$sortedArray = General::orderArray($unsortedArray,"down",$_GET['id']);
	
		if($sortedArray == false) {
			General::ouputMessage('Fehler','Die Box ist schon ganz rechts.','form');
		}else{
			// Save
			foreach($sortedArray as $orderId=>$id) {
				DB::getInstance()->query("
					UPDATE
						".Tables::$pageBoxContentTbl."
					SET
						orderId = '".$orderId."'
					WHERE
						id = '".$id."'");
			}
			General::ouputMessage('Erfolgreich','Die Box wurde erfolgreich verschoben.','form');
		}
	}
	
	
	General::getTpl()->assign('content',Pages::getBoxContent($_GET['boxId']));
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/pageBoxContent.html'));
}
?>